import numpy as np
import matplotlib.pyplot as plt
from rk45 import cal_temperature
import time
from data_record import data_record
R_1 = 0.0012
R_2 = 0.0093
C_in = 1100000
C_wall = 186000000
theta_out = -20
dt = 10

household_num = 600
run_time = 3600*4 #sec


cal_temp = []
dr = []
# init
for i in range(household_num):
    cal_temp.append(cal_temperature(dt, R_1, R_2, C_in, C_wall))
    dr.append(data_record(i,'test'))
    if(i%2 == 0):
        cal_temp[i].reset_B_P()
    else:
        cal_temp[i].reset_B_0()

    cal_temp[i].reset_out_tempurature(theta_out)
    cal_temp[i].set_start_theta(np.random.uniform(18, 22), 14.808)

# cal
#################################1
# limit_device_num = int(500)
limit_device_num = int(220)

out_flag = False


last_time = time.time()

for index in range(int(run_time/dt)):
    # if(out_flag is True):
    #     break
    all_temp = np.zeros((household_num))
    for i in range(household_num):
        pass

    if(index%(6)==0):
        for i in range(household_num):
            dr[i].data_append(index,cal_temp[i].get_theta()[0,0],cal_temp[i].get_theta()[1,0],cal_temp[i].P_status)

            all_temp[i] = cal_temp[i].get_theta()[0,0]
            cal_temp[i].reset_B_0()
            if(all_temp[i] < 18):
                out_flag = True
        result = np.argpartition(all_temp, limit_device_num)
        for k in result[:limit_device_num]:
            if(all_temp[k] < 22):
                cal_temp[k].reset_B_P()
    for i in range(household_num):
        cal_temp[i].rk45()

# read data and plot
first_any = True
Power_all = np.zeros((int((run_time)/dt/6)-1))
for i in range(household_num):
    # dr[i].data_dump()
    plot_data = dr[i].data_get()
    x_axis = plot_data[:,0]/360
    temp_in = plot_data[:,1]
    temp_wall = plot_data[:,2]
    P_status = plot_data[:,3]
    # last_P_status = P_status
    last_P_status = P_status[1:]
    change_value = abs(P_status[:-1] - last_P_status)
    change_value[0] = 0 

    Power_all = Power_all + change_value
    plt.plot(x_axis,temp_in)

plt.plot(x_axis[:-1],Power_all)

######################################
# clear dr
for i in range(household_num):
    dr[i].data_clear()
    cal_temp[i].set_start_theta(np.random.uniform(18, 22), 14.808)
    if(i%2 == 0):
        cal_temp[i].reset_B_P()
    else:
        cal_temp[i].reset_B_0()
######################################2
limit_device_num = int(400)

out_flag = False


last_time = time.time()

for index in range(int(run_time/dt)):
    # if(out_flag is True):
    #     break
    all_temp = np.zeros((household_num))
    for i in range(household_num):
        pass
    if(index%(6)==0):
        for i in range(household_num):
            dr[i].data_append(index,cal_temp[i].get_theta()[0,0],cal_temp[i].get_theta()[1,0],cal_temp[i].P_status)
            all_temp[i] = cal_temp[i].get_theta()[0,0]
            cal_temp[i].reset_B_0()
            if(all_temp[i] < 18):
                out_flag = True
        result = np.argpartition(all_temp, limit_device_num)
        for k in result[:limit_device_num]:
            # if(all_temp[k] < 22):
                cal_temp[k].reset_B_P()
    for i in range(household_num):
        cal_temp[i].rk45()


# read data and plot
first_any = True
Power_all = np.zeros((int((run_time)/dt/6)-1))
for i in range(household_num):
    # dr[i].data_dump()
    plot_data = dr[i].data_get()
    x_axis = plot_data[:,0]/360
    temp_in = plot_data[:,1]
    temp_wall = plot_data[:,2]
    P_status = plot_data[:,3]
    # last_P_status = P_status
    last_P_status = P_status[1:]
    change_value = abs(P_status[:-1] - last_P_status)
    change_value[0] = 0 
    Power_all = Power_all + change_value
    # print(change_value)
    plt.plot(x_axis,temp_in)

plt.plot(x_axis[:-1],Power_all)


plt.show()

